<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/22 0022
 * Time: 16:19
 */

namespace app\wap\controller;


use think\Cache;
use think\Db;
use Firebase\JWT\JWT;

class Home extends Base
{
    //todo banner图
    public function GetBanner(){
        $map = ['show'=>1];
        $list = Db::name('article')
            ->where($map)
            ->field('id,banner')
            ->select();
        if(!$list)return $this->returnError($this->NoData);
        return $this->returnSuccess($list);
    }
  
  	public function BannerDetail(){
    	$map = ['id'=>$this->data['id']];
      	$Key = config('LOGINKEY');
      	$ref = Db::name('article')
          ->where($map)
          ->find();
      	$token = input('token');
      	$ref['code'] = '';
      	if($token){
            $res = JWT::decode($token,$Key,['HS256']);
            if(!$token || !is_object($res))return $this->returnError('登录信息错误');
            $ref['code'] = Db::name('user')->where(['uid'=>$res->uid])->value('invitation');
        }
      	if(!$ref)return $this->returnError($this->Error);
      	return $this->returnSuccess($ref);
    }
  
    //todo 系统通知
    public function GetTips(){
        $list = $this->DbCommon($this->Key,2);
        if(!$list)return $this->returnError($this->NoData);
        return $this->returnSuccess($list);
    }
    //todo 首页的实时数据的币种
    public function WapSixData(){
        $list = Cache::get('huobi');
        if(!$list)return $this->returnError($this->NoData);
        $array = [];
        foreach ($list as $k=>$v){
            $map = ['name'=>$v['name']];
            $price = Db::name('data')->where($map)->find();
            if($price['name'] == $v['name']){
                $array[$k]['lv'] =  ($v['price'] - $price['close'])/$price['close'];
                $array[$k]['price'] = $v['price'];
                $array[$k]['cny'] =  $this->ChangeCNY($v['price']);
                $array[$k]['name'] = strtoupper($v['name']);
            }
        }
        return $this->returnSuccess($array);
    }
    //todo 历史回顾
    public function HistoryMessage(){
        $Key = config('LOGINKEY');
        $token = input('token');
        $time = Db::name('message')->order(['time desc'])->where(['type'=>0,'model'=>['in','1,2,7']])->value('time');
      	$map = ['type'=>0,'model'=>['in','1,2,7'],'time'=>['<',$time - 86400*2]];
        if($token){
            $ref = JWT::decode($token,$Key,['HS256']);
            if(!$token || !is_object($ref))return $this->returnError('登录信息错误');
            $vip = Db::name('user_service')->where(['uid'=>$ref->uid])->find();            
          	if($vip['vip'] > 0)$time = time()+3600;
          	$map['time'] = ['<',$time];
        }
        $limit = isset($this->data['limit']) ? $this->data['limit'] : 4;
      	$page = $this->page($this->data);
        $list = Db::name('message')
            ->where($map)
            ->limit($page['page'],$page['limit'])
            ->field('model,time,content,title')
            ->order('time desc')
            ->select();
        if($limit == 4)$list = array_slice($list,0,4);
        return $this->returnSuccess($list);
    }
    //todo 联系客服
    public function CustomerService(){
        $list = $this->DbCommon($this->Key,97);
        if(!$list)return $this->returnError($this->Error);
        $ref = [
            'sdesc'=>$list[0]['content'],
            'sqr'  =>$list[1]['content'],
            'gdesc'=>$list[2]['content'],
            'gqr'  =>$list[3]['content'],
        ];
        return $this->returnSuccess($ref);
    }
    //todo 关于我们客服
    public function AboutOur(){
        $list = $this->DbCommon($this->Key,81);
        if(!$list)return $this->returnError($this->Error);
        $res = explode(',',$list[0]['content']);
        $ref = ['name'=>$res[0],'company'=>$res[1],'edition'=>$res[2],'logo'=>$list[1]['content']];
        return $this->returnSuccess($ref);
    }

    //todo 获取声明  82免责 83服务
    public function GetStatement(){
        $type = ['type'=>$this->data['type']];
        $res = Db::name('common')->where($type)
            ->value('content');
        return $this->returnSuccess($res);
    }

    //todo 查询数据库
    private function DbCommon($key,$type){
      	$this->redis->delete($key);
        $list = unserialize($this->redis->get($key));
        if(!$list){
            $map = ['show'=>1,'type'=>$type];
            $list = Db::name('common')
                ->where($map)
                ->field('content')
                ->order('orderby asc')
                ->select();
            if(!$list)return false;
            $this->redis->set($key,serialize($list));
        }
        return $list;
    }

	public function GetDown(){
        $res = Db::name('common')
            ->where(['id'=>['in',[28,29]]])
            ->select();
      	$list['android'] = $res[0]['content'];
      	$list['ios'] = $res[1]['content'];
        return $this->returnSuccess($list);
    }

}